<?php


namespace app\api\controller;

use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use custom\Err;
use custom\Redis;
use push\Aurora;
use stdClass;
use think\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class Test extends Controller {

    public function test() {
        $data = db('house_auction')->field('id,initial_price,predict_price,price_lower_offset,bond,consult_price,market_price')->select();
        $arr = [];
        foreach ($data as $v) {
            if ($v['initial_price'] % 100 > 0) {
                $arr[] = $v;
                continue;
            }
            if ($v['price_lower_offset'] % 100 > 0) {
                $arr[] = $v;
                continue;
            }
            if ($v['predict_price'] % 100 > 0) {
                $arr[] = $v;
                continue;
            }
            if ($v['bond'] % 100 > 0) {
                $arr[] = $v;
                continue;
            }
            if ($v['consult_price'] % 100 > 0) {
                $arr[] = $v;
                continue;
            }
            if ($v['market_price'] % 100 > 0) {
                $arr[] = $v;
                continue;
            }

        }
        $this->resData('success', $arr);
        Err::add('aa');
        $a = Err::getMessage();
        print_r($a);
        exit;
        echo floatInt(3407465.6, 2);
    }

    /**
     * 只能选房信息
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function houseMsg() {
        // 获取用户的 registration_id
        if (!$userData = db('user')->field('registration_id,id')->select()) {
            $this->resErr('不存在用户');
        }
        // 随机房源id
        $limit = rand(1, 20);
        $houseIds = db('house')->limit($limit)->column('id');
        $count = count($houseIds);
        $pushMsg = [];
        foreach ($userData as $key => $user) {
            $data = [
                'title' => "已为您找到{$count}套房源",
                'house' => implode(',', $houseIds),
                'registration_id' => $user['registration_id'],
                'user_id' => $user['id'],
                'itime' => time(),
            ];

            if (db('message_user_house')->insertGetId($data) > 0) {
                $user['registration_id'] > 0 && $pushMsg[] = $user['registration_id'];
            }
        }

        if (!empty($pushMsg)) {
            $cids = Aurora::getCid(1);
            isset($cids[0]) && Aurora::push($cids[0], $pushMsg, "已为您找到{$count}套房源", '');
        }

        $this->resData('success');
    }

    /**
     * 系统信息
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function systemMsg() {
        $field = 'title,content,type,url';
        $params = $this->request->param();

        // 获取用户的 registration_id
        if (!$userData = db('user')->field('registration_id,id')->select()) {
            $this->resErr('不存在用户');
        }

        foreach ($userData as $key => $user) {
            $data = [
                'title' => $params['title'],
                'content' => $params['content'],
                'user_id' => $user['id'],
                'type' => $params['type'],
                'url' => $params['url'],
                'itime' => time(),
            ];

            if (db('message_user_system')->insertGetId($data) > 0) {
                $user['registration_id'] > 0 && $pushMsg[] = $user['registration_id'];
            }
        }

        if (!empty($pushMsg)) {
            $cids = Aurora::getCid(1);
            isset($cids[0]) && Aurora::push($cids[0], $pushMsg, $params['title'], '');
        }

        $this->resData('success');
    }

    /**
     * redis队列测试
     */
    public function addQueue() {
        $params = $this->request->param();
        $redisModel = Redis::getInstance();
        $redisModel->select(2);
        $redisModel->lpush(RedisKey::HOUSE_QUEUE, json_encode($params));
    }

    /**
     * api接口错误返回
     * @param $msg
     * @param int $code
     * @param string $fields
     * @return false|string
     */
    protected function resErr($msg = '', $code = RespCode::DEFAULT_ERR, $fields = '*') {
        $code == 0 && $code = RespCode::DEFAULT_ERR;
        $data = new stdClass();
        $time = time();
        $result = compact('code', 'msg', 'data', 'time', 'fields');
        if ($msg == '') {
            $result = array_merge($result, $this->getErrInfo());
        }
        json($result)->send();
        exit;
    }

    /**
     * 错误数据处理
     * @return array
     */
    protected function getErrInfo() {
        $msgInfo = Err::getMessage();
        if (!isset($msgInfo[0])) {
            return [
                'msg' => '',
                'code' => 400,
                'fields' => '*'
            ];
        }
        return $msgInfo[0];
    }

    /**
     * api接口正确返回
     * @param $msg
     * @param array $data
     * @param int $code
     * @return false|string
     */
    protected function resData($msg, $data = [], $isArray = false, $code = RespCode::SUCCESS) {
        !$isArray && $data = empty($data) ? new stdClass() : $data;
        $time = time();
        $result = compact('code', 'msg', 'data', 'time');
        json($result)->send();
        exit;
    }

}
